//前端异步获取登录信息
$(function(){
	//获取登录信息
	$.ajax({
		url: "/_tif_sso_loginuser_get/",
		method: "POST",
		success:function(result){
			if(result && result.errcode=="0") {
				var user = result.data;
				var name = user.name;
				var level = user.level;
				var userType = "个人";
				var currentlogin = "";
         		
         		//如果有父账号，显示父账号名称
         		if(user.corp) {
         			currentlogin = "当前登录："+user.name;
         			level = user.corp.level;
         			name = user.corp.name;
         			userType = "企业";
         		}
         		//企业账号
         		if(user.account_type == 'corp') {
         			userType = "企业";
         		}
         		
         		//消息通知
         		var personal_html = '<li id="login-li" class="gd-md-hide">' +
         							'<a href="'+xxzxUrl+'" rel="noopenner noreferrer" id="userpanel-notification" class="gd-userpanel-notification" title="消息通知"></a></li>' +
         							'<li><div class="gd-userpanel gd-userpanel-personal">' +
         							'<a href="javascript:;" class="gd-userpanel-toggle"><span>{name}</span></a>' +
         							'<ul class="gd-userpanel-dropdown">' +
         							'<li class="gd-userpanel-username">' +
         							'<span class="gd-userpanel-name">{name}</span>' +
         							'<span class="gd-userpanel-level">{userLevel}</span>' +
         							'<span class="gd-userpanel-currentlogin">{currentlogin}</span>' +
         							'</li>'+
         							'<li class="gd-userpanel-ucenter">' +
         							'<a href="'+grzxUrl+'" rel="noopenner noreferrer">{userType}中心</a></li>'+
         							'<li class="gd-userpanel-manage"><a href="'+zhglUrl+'" rel="noopenner noreferrer">账户管理</a></li>' +
         							'<li class="gd-userpanel-logout"><a href="javascript:;" onclick="gotoLogout()">退出登录</a></li>' +
         							'</ul></div></li>';
				//首页
         		$(".loginUserNameWithCommaSpan").html(name+"，");
         		//右上角登录框
         		personal_html = personal_html.replace(/{userType}/g, userType)
         									 .replace(/{userLevel}/g, level)
         									 .replace(/{name}/g, name)
         									 .replace(/{currentlogin}/g, currentlogin);
         		var login = $("#login-li");
         		login.after(personal_html);
         		login.remove();
         		//移动端登录按钮
         		document.getElementById("login_mobile_button").removeEventListener("click", function(){}, false);         		
         		$("#login_mobile_button").click(function(){
         			location.href = grzxUrl;
         		});
         		//保存用户对象
         		loginInfo = user;
         		//获取消息中心通知数量
         		getUserCenterCount();
			}
		}
	});
});

function getUserCenterCount() {
	if(typeof(loginInfo) == 'undefined' || typeof(xxzxCountUrl) == 'undefined' || !xxzxCountUrl) {
		console && console.log("用户中心地址缺失");
		return;
	}
	$.ajax({
		url: xxzxCountUrl,
		type: "POST",
		data: {
			flag : 0
		},
		success: function(result){
			try{
				if(result && result.errcode == '0' && result.data.count > 0) {
					var count = result.data.count;
					if(count > 99){
						$("#userpanel-notification").append("<span>"+99+"+</span>");
					} else {
						$("#userpanel-notification").append("<span>"+count+"</span>");
					}
				}
			} catch(err) {
				console && console.log("用户中心通知数量获取失败："+err.message);
			}
		},
        error: function (status) {
        	console && console.log("用户中心通知数量获取失败："+status);
        }
	});
}

function gotoLogin() {
	//paasid定义在头文件
	//var paasid = location.pathname.split('/')[1];
	if(typeof(paasid) == 'undefined' || !paasid) {
		var paasid = location.pathname.split('/')[1];
	}
	var selfUrl = location.pathname+location.search+location.hash;
	var gotoUrl = '/_in_/?r=' + encodeURIComponent(selfUrl) + '&p=portal';
	location.href = gotoUrl;
}

function gotoLogout() {
	//paasid定义在头文件
	//var paasid = location.pathname.split('/')[1];
	if(typeof(paasid) == 'undefined' || !paasid) {
		var paasid = location.pathname.split('/')[1];
	}
	var selfUrl = location.pathname + location.search + location.hash;
	var logoutUrl = '/_out_/?p=portal&r=' + encodeURIComponent(selfUrl); 
	location.href = logoutUrl;
}